﻿package 
{
    import flash.display.Sprite;
    import flash.geom.Point;

    public class WallWithDoor extends Wall
    {
        private var m_firstRoomNumber:uint;
        private var m_secondRoomNumber:uint;

        private var m_door:Sprite = new Sprite();

        private var m_x1:Number;
        private var m_x2:Number;

        public function WallWithDoor(firstRoomNumber:uint, secondRoomNumber:uint)
        {
            super();
            m_firstRoomNumber = firstRoomNumber;
            m_secondRoomNumber = secondRoomNumber;

            m_door.graphics.beginFill(0xff0000);
            m_door.graphics.lineStyle(GameConstants.DOOR_THICKNESS, 0xff0000);
            
            m_x1 = (GameConstants.WIDTH - GameConstants.DOOR_WIDTH / 2) / 2;
            m_x2 = (GameConstants.WIDTH + GameConstants.DOOR_WIDTH / 2) / 2;
            
            m_door.graphics.moveTo(m_x1, 0);
            m_door.graphics.lineTo(m_x2, 0);
            m_door.graphics.endFill;

            addChild(m_door);
        }

        private function getNeighborRoomNumber(currRoomNumber:uint):uint
        {
            if (m_firstRoomNumber == currRoomNumber)
            {
                return m_secondRoomNumber;
            }
            else if (m_secondRoomNumber == currRoomNumber)
            {
                return m_firstRoomNumber;
            }
            else 
            {
                throw new ArgumentError("Incorrect current room number");
            }
        }

        override public function enter(player:Player, maze:Maze):void
        {
            var currX:Number = player.getPlayer().x;
            var currY:Number = player.getPlayer().y;
            var newX:Number = Math.abs(currX - GameConstants.WIDTH + 2 * GameConstants.WALL_THICKNESS);
            var newY:Number = Math.abs(currY - GameConstants.HEIGHT + 2 * GameConstants.WALL_THICKNESS);
            
            if (newX < GameConstants.WALL_THICKNESS)
            {
                newX += GameConstants.WALL_THICKNESS;
            }
            if (newY < GameConstants.WALL_THICKNESS)
            {
                newY += GameConstants.WALL_THICKNESS;
            }
            
            player.setPosition(new Point(newX, newY));

            maze.showRoom(getNeighborRoomNumber(maze.getCurrRoom().roomNumber));
        }

        /*public function getDoorStartPos():Point
        {
            var x1:Number = super.x + Math.cos((super.rotation * Math.PI) / 180) *  m_x1;
            var y1:Number = super.y + Math.sin((super.rotation * Math.PI) / 180) * m_x1;

            return new Point(x1, y1);
        }

        public function getDoorEndPos():Point
        {
            var x1:Number = super.x + Math.cos((super.rotation * Math.PI) / 180) *  m_x2;
            var y1:Number = super.y + Math.sin((super.rotation * Math.PI) / 180) * m_x2;

            return new Point(x1, y1);
        }*/
    }
}
